﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using FRS_LIB.Devices;

namespace FRS_LIB
{
    public enum DeviceType
    {
        /// <summary>
        /// Has 14 usable port (0..5 230V, 6..13 24V)
        /// </summary>
        Switchbox = 0,
        /// <summary>
        /// Has 8 usable port (0..7 24V)
        /// </summary>
        CECC = 1,
        /// <summary>
        /// Has 4 usable port (0..3 230V)
        /// </summary>
        NETIO_230B = 2,
    }
    public class FRS
    {


        private Dictionary<String, DeviceBase> Devices = new Dictionary<string, DeviceBase>();
        public FRS()
        {
        }

        /// <summary>
        /// Add a new control device to the FRS
        /// </summary>
        /// <param name="address">IP-address of the controldevice</param>
        /// <param name="name">Free configurable name for the device (is used to use Get- and SetOutput</param>
        /// <param name="type">Type of the controldevice</param>
        public void AddDevice(string address, string name, DeviceType type)
        {
            DeviceBase dev;
            IPAddress.Parse(address);
            switch (type)
            {
                case DeviceType.CECC:
                    dev = new CECC(address, name);
                    break;
                case DeviceType.NETIO_230B:
                    dev = new NETIO_230B(address, name);
                    break;
                case DeviceType.Switchbox:
                    dev = new SwitchBox(address, name);
                    break;
                default:
                    throw new Exception("The type does not match one of the available types (CECC, SwitchBox, NETIO_230B)!");
            }
            if (!Devices.ContainsKey(name))
                Devices.Add(name, dev);
            else
                throw new Exception("There is already a device defined with the specified name");
        }

        /// <summary>
        /// Gets the information of a port, wether it's on or off
        /// </summary>
        /// <param name="name">Name of the device, that has been used to add the device</param>
        /// <param name="index">Index of the pin</param>
        /// <returns>true when pin is on, false otherwise</returns>
        public bool GetDeviceStatus(string name, int index)
        {
            try
            {
                return Devices[name].GetOutput(index);
            }
            catch (KeyNotFoundException ex)
            {
                throw new Exception(String.Format("The device with the name {0} wasn't added with AddDevice", name), ex);
            }
        }

        /// <summary>
        /// Gets the information of a port, wether it's on or off
        /// </summary>
        /// <param name="turnOn">if true pin will be turned on, if false the pin will be turned off</param>
        /// <param name="name">Name of the device, that has been used to add the device</param>
        /// <param name="index">Index of the pin</param>
        public void TurnOnOffDevice(bool turnOn, string name, int index)
        {
            try
            {
                Devices[name].SetOutput(index, turnOn ? 1 : 0);
            }
            catch (KeyNotFoundException ex)
            {
                throw new Exception(String.Format("The device with the name {0} wasn't added with AddDevice", name), ex);
            }
        }
    }
}
